const api = require("../../utils/cloudAPIUtils");
const app = getApp();

Page({
    data: {
        mapObj:null,
        myLongitude: '', // 经度
        myLatitude: '', // 纬度
        markers:[],
        tabBar:[
            {key:'全部',pox:-280},
            {key:'充电插座',pox:-110},
            {key:'充电宝',pox:60},
            {key:'便携电池',pox:230},
        ],
        tabBarIndex:0,
        nearPlace:[],
        markertap:{   //点击的设备信息
            // id:123456
        }, 
    },
    btnSettingUse(e){
        console.log(e)
        wx.showModal({
          title: '常用设备',
          content: '设置为常用设备？',
          complete: (res) => {
            if (res.confirm) {
                api.post(api.url.useDeviceAdd,{
                    placeId:e.currentTarget.dataset.place
                },res=>{
                    if(res.data.code==200){
                        wx.showToast({title: '设置成功！',icon:'none'})
                    }else{
                        wx.showToast({title: res.data.msg,icon:'error'})
                    }
                })
            }
          }
        })
    },
    btnChangeTab(e){
        this.setData({
            tabBarIndex:e.currentTarget.dataset.index
        })
    },
    btnPlaceNavigation(){
        const markertap = this.data.markertap;
        wx.openLocation({
            latitude: markertap.latitude,
            longitude: markertap.longitude,
            name: markertap.placeName,
            address: markertap.address
        })
    },

    /**
     * 获取附近网点信息
     */
    _findNearby(latitude,longitude){
        const self = this;
        const markers = [];
        api.post(api.url.commonNearPlace,{
            longitude:longitude,
            latitude:latitude,
            limit:10
        },res=>{
            console.log(res)
            if(res.data.code==200){
                for (const item of res.data.body.items) {
                    markers.push({
                        id:item.placeId,
                        latitude:item.latitude,
                        longitude:item.longitude,
                        width:30,height:30,
                        iconPath:'/static/ico/icon.png',
                    })
                }
                self.setData({
                    markers:markers,
                    nearPlace:res.data.body.items||[]
                })
            }
        })
    },
    btnClear(){
        this.setData({markertap:{}})
    },
    btnMarkertap(e){
        const id = e.detail.markerId;
        const self = this;
        for (const item of this.data.nearPlace) {
            if(item.placeId == id){
                self.setData({markertap:item})
                return;
            }
        }
    },
    btnRegionchange(e){
        this.setData({markertap:{}})
        if(e.type =='end' && e.causedBy == 'drag'){
            this.data.mapObj.getCenterLocation({
                success:(res)=>{
                    this._findNearby(res.latitude,res.longitude)
                }
            })
        }
    },
    bindMyLocation(){
        wx.getSetting({
            success:(res)=>{
                if(!res.authSetting['scope.userLocation']){
                    this.openAddressConfirm();
                }else{
                    this.data.mapObj.moveToLocation();
                    const {myLatitude,myLongitude} = this.data;
                    this._findNearby(myLatitude,myLongitude)
                }
            }
        })
    },
    openAddressConfirm: function () {
        wx.showModal({
          content: '检测到您没打开定位权限，是否去设置打开？',
          confirmText: "确认",
          cancelText: "取消",
          success: function (res) {
            //点击“确认”时打开设置页面
            if (res.confirm) {
              wx.openSetting({
                success: (res) => {
                  if (res.authSetting["scope.userLocation"] == true) {}
                }
              })
            }
          }
        });
    },
    onLoad(options) {
        this.setData({
            mapObj:wx.createMapContext('wxMap')
        })
        wx.getLocation({
            type:'gcj02',
            success:(res)=>{
                this.setData({
                    myLongitude:res.longitude,
                    myLatitude:res.latitude
                })
                this._findNearby(res.latitude,res.longitude)
            }
        })
    },
})